package demo1229.lianxi1;


public class Customer extends Thread {

    private BaoZi baoZi;

    public Customer(BaoZi baoZi) {
        this.baoZi = baoZi;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (baoZi) {

                //判断包子不存在
                if (!baoZi.isExist()) {
                    try {
//                 顾客进入等待状态
                        baoZi.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //包子存在
//              吃包子
                System.out.println("顾客开始吃" + baoZi.getPi() + baoZi.getXian()
                        + "包子,吃了3秒钟");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //把包子的状态设置为没有包子.
                baoZi.setExist(false);
                System.out.println("顾客包子吃完了");
                //包子吃完了,通知老板做包子.
                baoZi.notify();
                System.out.println("====================");
            }
        }
    }
}
